package com.rc.evcharger.service.discount;

import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.mapper.discount.TenantDiscountTimeMapper;
import com.rc.evcharger.model.discount.TenantDiscountTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

/**
 *
 * Created by sven on 2019/09/23.
 */
@Service
public class TenantDiscountTimeServiceImpl implements TenantDiscountTimeService {

	@Autowired
    private TenantDiscountTimeMapper tenantDiscountTimeMapper;

    @Override
    public TenantDiscountTimeMapper getMapper() {
       return tenantDiscountTimeMapper;
    }

    @Override
    public TenantDiscountTime selectValidTime(Long discountPolicyId, String time) {
        return tenantDiscountTimeMapper.selectValidTime(discountPolicyId,time);
    }

    @Override
    public String selectPrices(Long discountPolicyId, String priceType) {
        List<TenantDiscountTime> list = tenantDiscountTimeMapper.list(discountPolicyId);
        if (CollectionUtils.isEmpty(list)){
            return "无";
        }

            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            StringBuffer sb = new StringBuffer();

            Iterator<TenantDiscountTime> iterator = list.iterator();
            if (priceType.equals(SocketConstant.STR_CON1)) {
                while (iterator.hasNext()) {
                    TenantDiscountTime next = iterator.next();
                    sb.append(sdf.format(next.getBeginTime())).append("~")
                            .append(sdf.format(next.getEndTime())).append(" ")
                            .append(next.getElecPrice()).append("元/度")
                            .append("#");
                }
            } else {
                while (iterator.hasNext()) {
                    TenantDiscountTime next = iterator.next();
                    sb.append(sdf.format(next.getBeginTime())).append("~")
                            .append(sdf.format(next.getEndTime())).append(" ")
                            .append(next.getServicePrice()).append("元/度")
                            .append("#");
                }
            }

            return sb.toString().substring(0, sb.length() - 1);
    }
}
